import { message } from 'antd'
import axios from './http'
import api from './config'

const { apiurl, request } = axios

const gen = params => {
  let url = apiurl + params
  let method = 'GET'
  const paramsArray = params.split(' ')
  if (paramsArray.length === 2) {
    method = paramsArray[0]
    url = paramsArray[1]
    if (!(url.indexOf('://') > -1)) {
      url = apiurl + url
    }
  }

  return function (data) {
    request({
      url,
      data,
      method,
    })
    .then(res => {
      if(!res.success) {
        message.error(res.data)
      }
    })
    return request({
      url,
      data,
      method,
    })
  }
}

const mock = params => {
  let url = params
  let method = 'POST'

  return function (data) {
    return request({
      url,
      data,
      method,
    })
  }
}

const APIFunction = {}
let key
for (key in api) {
  APIFunction[key] = gen(api[key])
}

export default APIFunction
